package com.first.agent.dao.impl;

import com.first.agent.dao.CloudDao;
import com.first.agent.dbutils.JdbcBuild;
import com.first.agent.pojo.Cloud;
import com.first.agent.pojo.gateway;

import java.sql.SQLException;
import java.util.List;

/**
 * @author 创建人名称
 * @version 1.0.0
 * @ClassName CloudDaoImpl.java
 * @Description TODO
 * @createTime 2022年11月15日 09:02
 */
public class CloudDaoImpl extends JdbcBuild implements CloudDao {
    /*
    门户管理默认显示数据，关键词查询，客服名称查询
    keyword关键词
    customer_name客户名称
     */
    @Override
    public List<gateway> selectGate(String kw_name, String customer_name, Integer page, Integer limit) throws SQLException {
        /*
        默认显示页面数据
         */
        if ((kw_name==null||kw_name=="")&&(customer_name==null||customer_name=="")){
            System.out.println("默认");
            String sql="SELECT * FROM keyword k,agent_customer a,cloud_info c WHERE k.kw_agent_id=a.cus_id AND cloud_id=k.kw_cloud_id limit ?,?";
            Object[] params={page,limit};
            return super.queryListEntity(gateway.class, sql, params);
        }
        /*
        关键字搜索
         */
        if(kw_name!=null&&customer_name.equals("")){
            System.out.println("关键字");
            String sql="SELECT * FROM keyword k,agent_customer a,cloud_info c WHERE k.kw_name=? AND k.kw_agent_id=a.cus_id AND c.cloud_id=k.kw_cloud_id LIMIT ?,?";
            Object[] params={kw_name,page,limit};
            return super.queryListEntity(gateway.class, sql,params);
        }
        /*
        客户搜索
         */
        if(kw_name.equals("")&&customer_name!=null){
            System.out.println("客户");
            String sql="SELECT * FROM keyword k,agent_customer a,cloud_info c WHERE customer_name=? AND k.kw_agent_id=a.cus_id AND c.cloud_id=k.kw_cloud_id LIMIT ?,?";
            Object[] params={customer_name,page,limit};
            return super.queryListEntity(gateway.class, sql,params);
        }
        /*
        同时满足关键字和客户名称的搜索
         */
        if (kw_name!=null&&customer_name!=null){
            System.out.println("关键字和客户");
            String sql="SELECT * FROM keyword k,agent_customer a,cloud_info c WHERE customer_name=? AND k.kw_name=? AND k.kw_agent_id=a.cus_id AND c.cloud_id=k.kw_cloud_id LIMIT ?,?";
            Object[] params={kw_name,customer_name,page,limit};
            return super.queryListEntity(gateway.class, sql,params);
        }
        return null;
    }
    /*
    计数
     */
    @Override
    public int count() throws SQLException {
        String sql="SELECT count(*) FROM keyword k,agent_customer a,cloud_info c WHERE k.kw_agent_id=a.cus_id AND cloud_id=k.kw_cloud_id";

        return super.findAllForInt(sql);
    }
    @Override
    public int counta(String kw_name, String customer_name) throws SQLException {
        System.out.println("进入计数");
        String sql="SELECT count(*) FROM keyword k,agent_customer a,cloud_info c WHERE k.kw_agent_id=a.cus_id AND cloud_id=k.kw_cloud_id ";
        if(kw_name!=""&&kw_name!=null){
            sql=sql+" and kw_name="+"\'"+kw_name+"\'";
        }
        if(customer_name!=""&&customer_name!=null){
            sql=sql+" and customer_name="+"\'"+customer_name+"\'";
        }
        int allForInt = super.findAllForInt(sql);
        return allForInt;
    }
    /*
    移动云端信息查询
     */
    @Override
    public List<Cloud> selectCloud() throws SQLException {
        System.out.println("进入云端信息查询");
        String sql="select * from cloud_info ";
        Object[] params={};
        return super.queryListEntity(Cloud.class, sql,params);
    }
    /*
    移动云端信息修改
     */
    @Override
    public int changeCloud(Cloud cloud) throws SQLException {
        System.out.println("进入云端信息修改");
        String sql="update  cloud_info set cl_accout=? , cl_password =? , cl_dacstand=? , cl_dacstios=? , cl_qcdaand =? , cl_qcdaios=? where cloud_id=?";
        Object[] params={cloud.getCl_accout(),cloud.getCl_password(),cloud.getCl_dacstand(),cloud.getCl_dacstios(),cloud.getCl_qcdaand(),cloud.getCl_qcdaios(),cloud.getCloud_id()};
        return super.executeUpdate(sql, params);
    }
    /**
     * 添加移动云端
     */
    @Override
    public int addCloud(Cloud cloud) throws SQLException {
        System.out.println("进入云端信息添加");
        String sql="INSERT INTO cloud_info (cl_accout,cl_password,cl_dacstand,cl_dacstios,cl_qcdaand,cl_qcdaios,cl_address) VALUES (?,?,?,?,?,?,?)";
        Object[] params={cloud.getCl_accout(),cloud.getCl_password(),cloud.getCl_dacstand(),cloud.getCl_dacstios(),cloud.getCl_qcdaand(),cloud.getCl_qcdaios(),cloud.getCl_address()};
        int i = super.executeUpdate(sql, params);
        return i;
    }
    /**
     * 删除移动云端
     */
    @Override
    public int del(Integer id) throws SQLException {
        String sql="delete from cloud_info where cloud_id=?";
        Object[] params={id};
        int i = super.executeUpdate(sql, params);
        return i;
    }
}
